#include "frame.h"

#include <QTextStream>
#include <QApplication>
#include "utils.h"
#include "netspeed.h"

Frame::Frame(QWidget *parent) :
    QFrame(parent)
{
    this->setMaximumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->setMinimumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->resize(this->maximumSize());

    m_net_send_speed = boost::make_shared<QLabel>(this);
    m_net_send_speed->setAttribute(Qt::WA_TransparentForMouseEvents);
    m_net_send_speed->setMaximumSize(100, 20);
    m_net_send_speed->setMinimumSize(100, 20);

    m_net_recv_speed = boost::make_shared<QLabel>(this);
    m_net_recv_speed->setAttribute(Qt::WA_TransparentForMouseEvents);
    m_net_recv_speed->setMaximumSize(100, 20);
    m_net_recv_speed->setMinimumSize(100, 20);
    m_net_recv_speed->setAlignment(Qt::AlignRight);

    m_net_layout = boost::make_shared<QHBoxLayout>(this);
    m_net_layout->setMargin(0);
    m_net_layout->setSpacing(0);
    m_net_layout->addWidget(m_net_send_speed.get());
    m_net_layout->addWidget(m_net_recv_speed.get());

    this->setLayout(m_net_layout.get());
}

utils::Speed_percent Frame::read_net()
{
    QFile proc_file("/proc/net/dev");
    if(! proc_file.open(QFile::ReadOnly | QFile::Text)) {
        syslog(LOG_ERR, "Read proc file Failure, Please Check it !!!\n");
        exit(-1);
    }
    QTextStream stream(&proc_file);
    QApplication::setOverrideCursor(Qt::WaitCursor);

    /* Start Collect Data */
    QStringList lines;
    lines = stream.readAll().split("\n");

    QApplication::restoreOverrideCursor();
    proc_file.close();

    /* Becase First And Second Line is Desc, So We Drop It */
    lines.pop_front();
    lines.pop_front();

    /* All of Recv Data Length*/
    unsigned long long recv_data_len = 0;
    unsigned long long send_data_len = 0;

    for(QStringList::iterator it = lines.begin(); it != lines.end(); it ++) {
        if(! (*it).isEmpty()) {
            QStringList str = (*it).split(":").at(1).split(" ", QString::SkipEmptyParts);
            recv_data_len += str.at(0).toLongLong();
            send_data_len += str.at(8).toLongLong();
        }
    }

    struct utils::Speed_percent speed_percent;

    speed_percent.send_speed = (send_data_len - old_send_data_len) / 1024.0;
    speed_percent.recv_speed = (recv_data_len - old_recv_data_len) / 1024.0;

    /* Save date_len for next time */
    old_send_data_len = send_data_len;
    old_recv_data_len = recv_data_len;

    return speed_percent;
}

void Frame::update_net_gui(utils::Speed_percent &speed_percent)
{
    char text[64];
    /* Flush Up Speed GUI */
    bzero(text, sizeof(text));
    sprintf(text, "<span style='color: rgb(255, 103, 1);'>↑</span> %.1f ",
            speed_percent.send_speed > 1024.0 ? speed_percent.send_speed / 1024 : speed_percent.send_speed);
    m_net_send_speed->setText(QString::fromLocal8Bit(text) +
                              (speed_percent.send_speed > 1024.0 ? QString::fromLocal8Bit("Mb/s") : QString::fromLocal8Bit("Kb/s")));

    /* Flush Down Speed GUI */
    bzero(text, sizeof(text));
    sprintf(text, "<span style='color: green;'>↓</span> %.1f ",
            speed_percent.recv_speed > 1024.0 ? speed_percent.recv_speed / 1024 : speed_percent.recv_speed);
    m_net_recv_speed->setText(QString::fromLocal8Bit(text) +
                              (speed_percent.recv_speed > 1024.0 ? QString::fromLocal8Bit("Mb/s") : QString::fromLocal8Bit("Kb/s")));
}

void Frame::mouseDoubleClickEvent(QMouseEvent *)
{
    emit show_sysinfo_gui();
}
